package handler

import (
	"admin-ease/cache"
	"admin-ease/model"
	"admin-ease/pkg"
	"encoding/json"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {

	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段为空",
		})
		return
	}

	redis_login_key := "redis_login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_login_key).Int()

	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "错误三次",
		})
		return
	}

	var user model.User

	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}

	if user.Password != pkg.MD5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_login_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_login_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "错误三次，封禁两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "账号或密码错误",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
	})
	return

}

func List(c *gin.Context) {

	page, _ := strconv.Atoi(c.PostForm("page"))
	size, _ := strconv.Atoi(c.PostForm("size"))
	offset := (page - 1) * size

	/*
			SELECT
		  *
		FROM
		  user_roles
		  LEFT JOIN users ON users.id = user_roles.uid
	*/

	var uid []model.UserRole

	redis_list_key := "redis_list_" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_key).Result()

	if result == "" {
		model.DB.Table("user_roles").
			Joins("users.id = user_roles.uid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&uid)
		jsonData, _ := json.Marshal(uid)
		cache.Rdb.Set(cache.Ctx, redis_list_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &uid)
	}

	var total int64
	model.DB.Model(&model.UserRole{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表成功",
		"data":  uid,
		"total": total,
	})
	return

}

func UserList(c *gin.Context) {

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var user []model.User

	redis_list_key := "redis_list_" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_key).Result()

	if result == "" {
		model.DB.Offset(offset).Limit(size).Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_list_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	var total int64
	model.DB.Model(&model.User{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表成功",
		"data":  user,
		"total": total,
	})
	return

}

func UserDetail(c *gin.Context) {

	id := c.Query("id")

	var user model.User

	redis_detail_key := "redis_detail_" + id
	result, _ := cache.Rdb.Get(cache.Ctx, redis_detail_key).Result()

	if result == "" {
		model.DB.Where("id = ?", id).Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_detail_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	var total int64
	model.DB.Model(&model.User{}).Count(&total)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取详情成功",
		"data": user,
	})
	return

}
